let routes = require('./routes');
let userDao = require('../dao/userDao');
let playerDao = require('../dao/playerDao');
let Monster = require('../module/monster');
let mainHandle = require('../core/mainHandle');
let mapHandle = require('../core/mapHandle');
let totalConfig = mainHandle.totalConfig;
let propName = mainHandle.propName;
let mapListConfig = totalConfig.mapList;
let utils = require('../core/utils');

//获取冒险地图
routes.setLogin('getAdventrueMap', (session, msg, cb) => {
    let ret = utils.verification(msg, [
        ['mapId', 'number']
    ]);
    if (ret) {
        cb(102, ret);
        return;
    }
    let mapId = msg.mapId;
    let map = mapListConfig[mapId];
    if (!map) {
        cb(1);
        return;
    }
    cb(0, map)
}, {
    1: '地图id不存在'
});

//开始探索
routes.setLogin('startAdventrue', (session, msg, cb) => {
    let player = session.player;
    if (!player.basicInfo.map || player.basicInfo.map.length === 0) {
        cb(1)
    }
    let advMaps = utils.getRandomArray(3, player.basicInfo.map);
    player.basicInfo.advMaps = advMaps;
    cb(0, advMaps);
}, {
    1: '未生成地图'
});

//探索
routes.setLogin('explore', (session, msg, cb) => {
    let player = session.player;
    let data = mapHandle.explore(player);
    cb(0, data);
});

routes.setLogin('battle', (session, msg, cb) => {
    let player = session.player;
    player.initFightAttri()
    cb(0, {
        player: player,
        monster: new Monster(320001)
    })
});

//获取地图信息
routes.setLogin('getMapInfo', (session, msg, cb) => {
    let player = session.player;
    cb(0, player.basicInfo.mapInfo);
    return;
});

//拾取获取的物品
routes.setLogin('getItems', (session, msg, cb) => {
    let player = session.player;
    let mapInfo = player.basicInfo.mapInfo;
    if (!mapInfo) {
        cb(1);
        return;
    }
    let index = msg.index;
    let item = mapInfo.item;
    if (!item) {
        cb(2);
        return;
    }
    if (player.bag.addItem(item) !== -1) {
        mapInfo.item = null;
        cb(0);
        return;
    } else {
        cb(107);
        return;
    }
}, {
    1: 'mapInfo不存在',
    2: '物品不存在',
});